<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class ConditionController extends Controller
{
    //关联试验条件 值
    public function conditionVal(Request $request)
    {
        $id = $request->id; //a_condition_key ID

        if ($request->isMethod('POST')) {
            $condition_id = $request->condition_id;
            if ($condition_id) {
                $data = $request->only('vol_class_id');
                $res = DB::table('a_condition')->where('id',$condition_id)->update($data);
                return $res ? 1 : 0;
            }
            $data = $request->only(['vol_class_id', 'val']);
            $data['condition_key_id'] = $id;
            try {
                //插入数据    insert 返回true 或 false
                $result = DB::table('a_condition')->insert($data);
            } catch (\Throwable $e) {
                return '2';
            }
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        } else {
            // $data = DB::table('a_condition')->where('condition_key_id', $id)->orderBy('sort')->get();
            $sql1 = "SELECT
                        A.id,
                        A.vol_class_id,
                        A.val AS condition_val,
                        B.sort,
                        B.type,
                        B.val AS vol_class_val
                    FROM
                        a_condition A
                    LEFT JOIN a_voltage_class B ON A.vol_class_id = B.id
                    WHERE
                        A.condition_key_id = :id
                    ORDER BY
                        B.type,
                        B.sort";
            $data = DB::select($sql1, ['id' => $id]); //参数绑定可以避免 SQL 注入攻击

            $sql2 = "SELECT
                        A.id,
                        B.num,
                        C.`name` AS itemname,
                        D.`name` AS toolname,
                        A.`key`,
                        A.unit,
                        A.std_id,
	                    A.tool_id
                    FROM
                        a_condition_key A
                    LEFT JOIN a_standard B ON A.std_id = B.id
                    LEFT JOIN a_testitem C ON A.item_id = C.id
                    LEFT JOIN a_tool D ON A.tool_id = D.id
                    WHERE
                        A.id = :id";

            $data_rel = DB::select($sql2, ['id' => $id]); //参数绑定可以避免 SQL 注入攻击
            $data_rel = $data_rel[0];
            $data_vol_class = DB::table('a_voltage_class')->select('id', 'val')->where('std_id', $data_rel->std_id)->where('tool_id', $data_rel->tool_id)->orderBy('type')->orderBy('sort')->get();

            return view('admin.condition.conditionVal', compact('data_rel', 'data', 'data_vol_class'));
        }
    }

    //试验条件值 删除
    public function del(Request $request)
    {

        $id = $request->input('id');
        try {
            // delete 返回受影响的行数
            $result = DB::table('a_condition')->where('id', $id)->delete();
        } catch (\Throwable $e) {
            return '2';
        }
        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }

}
